package com.shuiqian.talentpool.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author landongqi
 * @create 2019-11-27 15:47
 */
public class MapUtil {


    public static <T> List<T> convertMapListToBeanList(List<Map> mapList, Class<T> clazz){
        List<T> list=new ArrayList<T>();
        for(Map map:mapList){
            try {
                T obj=clazz.newInstance();//创建bean的实例对象
                for(Object o:map.keySet()){//遍历map的key
                    if (o != null){
                        for(Method m:clazz.getMethods()){//遍历bean的类中的方法，找到set方法进行赋值
                            String s = m.getName().toLowerCase();
                            String s1 = o.toString().toLowerCase();

                            if(m.getName().toLowerCase().equals("set"+o.toString().toLowerCase())){
                                m.invoke(obj, map.get(o));
                            }
                        }
                    }
                }
                list.add(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
